<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增字段</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui/css/layui.css" media="all">
    <style type="text/css">
        .redstar{
            color: red;
            margin-right: 5px;
        }
        .layui-field-title{
            color: #ffffff;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form" lay-filter="columnInfo" action="" style="margin:10px 20px 0 10px">
            <input type="text" id="tableId" name="tableId" class="layui-input" style="display: none">
            <input type="text" id="columnId" name="columnId" class="layui-input" style="display: none">
<!--            <input type="text" id="modelName" name="modelName" class="layui-input" style="display: none">-->
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>字段名</label>
                <div class="layui-input-block">
                    <input type="text" id="columnCode" name="columnCode" lay-verify="required" autocomplete="off" placeholder="请输入字段名"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>字段中文名</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="columnName" name="columnName" lay-verify="required" autocomplete="off" placeholder="请输入字段中文名">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>类型
                </label>
                <div class="layui-input-block">
                    <select class="layui-select" id="type" name="type" lay-verify="required" lay-filter="type">
                        <option value="">请选择类型</option>
                        <option value="1">字符</option>
                        <option value="2">数字</option>
                        <option value="3">日期</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">
                    <i class="redstar">*</i>长度</label>
                <div class="layui-input-block">
                    <input class="layui-input" id="modelSize" name="modelSize" lay-verify="number" autocomplete="off" placeholder="请输入长度">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">小数</label>
                <div class="layui-input-block">
                    <input class="layui-input layui-disabled" id="modelFloat" name="modelFloat"  autocomplete="off" placeholder="请输入小数" disabled>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">默认值</label>
                <div class="layui-input-block">
                    <input type="text" id="modelDefault" name="modelDefault" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">是否可为空</label>
                <div class="layui-input-block">
                    <input type="radio"  name="isNull" value="T" title="是" checked>
                    <input type="radio"  name="isNull" value="F" title="否">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">是否为主键</label>
                <div class="layui-input-block">
                    <input type="radio"  name="isPrimary" value="1" title="是" >
                    <input type="radio"  name="isPrimary" value="0" title="否" checked>
                </div>
            </div>

            <!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label" style="padding: 9px 0;text-align: unset;margin-left: 10px">-->
<!--                    <i class="redstar">*</i>关联主数据-->
<!--                </label>-->
<!--                <div class="layui-input-block">-->
<!--                    <select class="layui-select" id="" name="" lay-verify="required">-->
<!--                        <option value="">请选择关联主数据</option>-->
<!--                    </select>-->
<!--                </div>-->
<!--            </div>-->
            <div class="layui-form-item layui-hide">
                <div class="layui-input-block">
                    <button id="yes" class="layui-btn yes" lay-submit lay-filter="submit" style="display: none">确定</button>
                </div>
            </div>
        </form>
    </div>
</div>


<script src="../lib/layui/layui.js" charset="utf-8"></script>
<script src="../js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="../js/common.js"></script>

<script>
    layui.use(['form','element','table'],function () {
        var element = layui.element,
            form = layui.form,
            table = layui.table;

        /**
         * 获取初始数据
         */
        var tableId = getUrlParams('tableId'),
            columnId = getUrlParams('columnId'),
            columnCode = getUrlParams('columnCode'),
            columnName = getUrlParams('columnName'),
            type = getUrlParams('type'),
            modelSize = getUrlParams('modelSize'),
            modelFloat = getUrlParams('modelFloat'),
            isNull = getUrlParams('isNull'),
            isPrimary = getUrlParams('isPrimary'),
            modelDefault = getUrlParams('modelDefault');
            // remark = getUrlParams('remark');

        $(document).ready(function () {
            if (columnId !== null && columnId !== undefined) {
                form.val('columnInfo', {
                    'tableId': tableId,
                    'columnId': columnId,
                    'columnCode':columnCode,
                    'columnName': columnName,
                    'type':type,
                    'modelSize': modelSize,
                    'isNull':isNull,
                    'isPrimary':isPrimary
                });
                if(modelFloat!=='null'){
                    form.val('columnInfo',{
                        'modelFloat':modelFloat
                    });
                }
                if(modelDefault!=='null'){
                    form.val('columnInfo',{
                       'modelDefault':modelDefault
                    });
                }
            } else {
                form.val('columnInfo', {
                    'tableId': tableId
                });
            }
        });

        /**
         * 配置约束
         */
        form.verify({
            number1:function (value,item){
                if(!isNaN(value)){
                    return '小数只可输入数字';
                }
            }
        });


        /**
         * 监听类型选择
         */
        form.on('select(type)',function (data){
            if(data.value==='2'){
                $('#modelFloat').removeClass('layui-disabled');
                $('#modelFloat').removeAttr('disabled');
                $('#modelFloat').attr('lay-verify','number1');

            }else{
                $('#modelFloat').addClass('layui-disabled');
                $('#modelFloat').attr('disabled',true);
                $('#modelFloat').removeAttr('lay-verify');

            }
        })
        /**
         * 监听提交按钮点击
         */
        window.addEventListener('message', function (event) {
            if (event.data === 'submit') {
                $("#yes").trigger("click");
            }
        });


        /**
         * 监听提交
         */
        form.on('submit(submit)', function (data) {
            if (data.field.columnId != null && data.field.columnId !== '') {
                /**
                 * 修改字段信息
                 */
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelTableColumn/updateModelColumn",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('update_success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('update_error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('update_error', '*');
                    }
                });
            } else {
                $.ajax({
                    type: "post",
                    url: "/baseDataManagerServer/modelTableColumn/insertModelColumn",
                    dataType: 'json',
                    cache: false,
                    contentType: "application/json",
                    data: JSON.stringify(data.field),
                    success: function (redata) {
                        if (redata.code === '0000') {
                            window.parent.postMessage('success-' + redata.msg, '*');
                        } else if (redata.code === '9999') {
                            window.parent.postMessage('error-' + redata.msg, '*');
                        }
                    },
                    error: function (redata) {
                        window.parent.postMessage('error', '*');
                    }
                });
            }
            return false;
        });
    });


    window.getUrlParams = function (key) {
        //获取url参数
        var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)", "i");
        var url = decodeURI(window.location.search)
        var r = url.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
</script>

</body>
</html>